﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FrontEndPlus.Módulos.Catálogos_y_Mantenimientos
{
    public partial class wbfrm_Mantenimiento_Salas : System.Web.UI.Page
    {
        #region Variables

        private WCF_Gimnasio.cls_Sala _sala = new WCF_Gimnasio.cls_Sala();
        private WCF_Gimnasio.ServiceClient _servicio = new WCF_Gimnasio.ServiceClient();

        private DataTable _registrosSala;

        #endregion

        #region Metodos

        private void CargarDatos()
        {
            _registrosSala = _servicio.Listar(1, _sala).Tables[0];

            _servicio.Close();
        }

        private void CrearObjeto()
        {
            if (gv_sala.Rows.Count != 0)
            {
                /* Columna: Id Sala */
                Response.Redirect("wbfrm_Cambio_Salas.aspx?idObjeto=" + Session["IdObjeto"].ToString(), true);
            }
        }

        private void VerficarBusqueda()
        {
            var valor = 1;
            int.TryParse(ddl_Tipo_Buscar.SelectedItem.Value, out valor);

            if (valor == 1)
            {
                if (Convert.ToInt32(txt_busqueda.Text) > 0)
                {
                    _sala.Filtros[0] = txt_busqueda.Text;
                    _sala.Filtros[1] = "Ft";
                }
            }

            else if (valor == 2)
            {
                _sala.Filtros[0] = "0";
                _sala.Filtros[1] = txt_busqueda.Text;
            }
        }

        #endregion

        #region Eventos

        protected void Page_Load(object sender, EventArgs e)
        {
            _sala.IdUsuarioInicioSeccion = Convert.ToInt32(HttpContext.Current.Session["idUsuario"].ToString());
            _sala.Filtros = new string[2];

            if (!IsPostBack)
            {
                CargarDatos();

                ViewState["RegistrosSala"] = _registrosSala;
            }

            else
            {
                _registrosSala = (DataTable)ViewState["RegistrosSala"];
            }

            gv_sala.DataSource = _registrosSala;
            gv_sala.DataBind();
        }

        protected void txt_busqueda_TextChanged(object sender, EventArgs e)
        {
            if (gv_sala.Rows.Count != 0)
            {
                if (!string.IsNullOrEmpty(txt_busqueda.Text) || !string.IsNullOrWhiteSpace(txt_busqueda.Text))
                {
                    try
                    {
                        VerficarBusqueda();

                        gv_sala.DataSource = _servicio.Listar(2, _sala);
                        gv_sala.DataBind();
                    }

                    catch (Exception)
                    {
                        string script = "<script type='text/javascript'> smoke.alert('Valor no permitido.', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
                    }
                }

                else
                {
                    CargarDatos();
                }
            }
        }

        protected void gv_sala_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_sala.PageIndex = e.NewPageIndex;

            CargarDatos();

            gv_sala.DataSource = _registrosSala;
            gv_sala.DataBind();
        }

        protected void gv_sala_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (gv_sala.Rows.Count != 0)
            {
                if (e.CommandName == "Seleccionar" || e.CommandName == "Select")
                {
                    var index = gv_sala.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text;
                    Session["IdObjeto"] = index;
                }
            }
        }

        protected void imgbtn_Agregar_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("wbfrm_Cambio_Salas.aspx", true);
        }

        protected void imgbtn_Modificar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (Session["IdObjeto"] != null)
                {
                    CrearObjeto();
                }

                else
                {
                    string script = "<script type='text/javascript'> smoke.alert('Debe seleccionar un registro para completar la accion.', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
                }
            }

            catch (Exception)
            {
                string script = "<script type='text/javascript'> smoke.alert('Se presento un incoveniente. Intente otra vez', function (e) {}, {ok: 'OK',classname: 'custom-class'}); </script>";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false);
            }
        }

        #endregion

    }
}
